import React from 'react'
import PropTypes from 'prop-types'
import {routerRedux} from 'dva/router'
import {connect} from 'dva'
import {Button, Col, message, Popconfirm, Row} from 'antd'
import {Page} from 'components'
import queryString from 'query-string'
import List from './components/List'
import Filter from './components/Filter'
import Modal from './components/Modal'



const Mobile = ({
                    location, dispatch, mobile, loading,
                  }) => {
  const {query, pathname} = location
  const {
    list, pagination, currentItem, modalVisible, disable, modalType, selectedRowKeys, empDataSource: dataSource,
  } = mobile

  const handleRefresh = (newQuery) => {
    dispatch(routerRedux.push({
      pathname,
      search: queryString.stringify({
        ...query,
        ...newQuery,
      }),
    }))
  }

  const searchEmp = (key) => {
    if (key) {
      dispatch({
        type: 'mobile/searchEmp',
        payload: {
          key,
        },
      })
    }
  }

  const modalProps = {
    item: modalType === 'bind' ? {} : currentItem,
    visible: modalVisible,
    dataSource,
    handleSearch: searchEmp,
    disable,
    width: 820,
    maskClosable: false,
    confirmLoading: loading.effects[`mobile/${modalType}`],
    title: `${modalType === 'bind' ? '绑定设备' : disable ? '手机详情' : '编辑设备信息'}`,
    wrapClassName: 'vertical-center-modal',
    onOk (data) {
      if (disable) {
        dispatch({
          type: 'mobile/hideModal',
        })
      } else {
        dispatch({
          type: `mobile/${modalType}`,
          payload: data,
        })
          .then(() => {
            handleRefresh()
            message.success("操作成功")
          })
      }
    },
    onCancel () {
      dispatch({
        type: 'mobile/hideModal',
      })
    },
  }



  const listProps = {
    rowKey: 'id',
    dataSource: list,
    loading: loading.effects['mobile/query'],
    pagination,
    location,
    onChange (page) {
      handleRefresh({
        current: page.current,
        size: page.pageSize,
      })
    },
    onDeleteItem (id) {
      dispatch({
        type: 'mobile/delete',
        payload: id,
      }).then(() => {
          handleRefresh({
            current: (list.length === 1 && pagination.current > 1) ? pagination.current - 1 : pagination.current,
          })
          message.success("删除成功")
        })
    },
    onEditItem (item, disable) {
      dispatch({
        type: 'mobile/showModal',
        payload: {
          modalType: 'update',
          currentItem: item,
          disable,
        },
      })
    },
    rowSelection: {
      selectedRowKeys,
      onChange: (keys) => {
        dispatch({
          type: 'mobile/updateState',
          payload: {
            selectedRowKeys: keys,
          },
        })
      },
      onSelect: (record) => {
        dispatch({
          type: 'mobile/updateState',
          payload: {
            selectRecord: record,
          },
        })
      },
    },
  }



  const filterProps = {
    dataSource,
    handleSearch : searchEmp,
    deviceBind () {
      dispatch({
        type: 'mobile/showModal',
        payload: {
          modalType: 'bind',
        },
      })
    },
    filter: {
      ...query,
    },
    onFilterChange (value) {
      handleRefresh({
        ...value,
        current: 1,
      })
    },
    onAdd () {
      dispatch({
        type: 'mobile/showModal',
        payload: {
          modalType: 'create',
          disable: false,
        },
      })
    },
  }

  const handleDeleteItems = () => {
    dispatch({
      type: 'mobile/multiDelete',
      payload: {
        ids: selectedRowKeys,
      },
    })
      .then(() => {
        handleRefresh({
          current: (list.length === selectedRowKeys.length && pagination.current > 1) ? pagination.current - 1 : pagination.current,
        })
        message.success("商品删除成功")
      })
  }



  return (
    <Page inner>
      <Filter {...filterProps} />
      {
        selectedRowKeys.length > 0 &&
        <Row style={{marginBottom: 24, textAlign: 'right', fontSize: 13}}>
          <Col>
            {`选中了 ${selectedRowKeys.length} 条数据`}
            <Popconfirm title="确定要删除这些数据吗?" placement="left" onConfirm={handleDeleteItems}>
              <Button type="danger" style={{marginLeft: 8}}>删除</Button>
            </Popconfirm>
          </Col>
        </Row>
      }
      <List {...listProps} />
      {modalVisible && <Modal {...modalProps} />}
    </Page>
  )
}

Mobile.propTypes = {
  employee: PropTypes.object,
  location: PropTypes.object,
  dispatch: PropTypes.func,
  loading: PropTypes.object,
}

export default connect(({mobile, loading}) => ({mobile, loading}))(Mobile)
